<?php
/**
 * @file $HeadURL: productcategorydefine $
 * @author $Author: LDD
 * @date $Date: 2014-12-17
 * @brief 
 */
namespace haibao\cms\business;

use \haibao\cms\data\ProductCategoryDefine as CategoryData;
use \haibao\cms\data\ProductCategoryProperty as PropertyData;
use \haibao\cms\model\filter\ProductCategoryDefine as ProductCategoryFilter;
use \haibao\cms\model\filter\ProductCategoryProperty as CategoryPropertyFilter;
use \haibao\frame\data\query\Condition ;

class ProductCategoryDefine extends \haibao\cms\business\Base{
	/**
	 * 查询产品分类树形结构
	 * @return array
	 */
	public static function getCategoryTree(){
		$categoryData = new CategoryData();
		return $categoryData->getCategoryTree();
	}
	
	/**
	 * 根据产品分类id查找
	 */
	public static function getCategoryById($categoryId){
		if(!$categoryId){
			throw new BusinessException('分类id不能为空',Base::DATA_EMPTY_ID);
		}
		$categoryData = new CategoryData();
		return $categoryData->getCategoryById($categoryId);
	}
	
	/**
	 * 查询所有分类
	 * @return array
	 */
	public static function getAllCategorys(){
		$data = new CategoryData();
		return $data->getAllCategorysDict();
	}
	/**
	 * @param $categoryName 分类名称
	 * @param $categoryId  分类Id
	 */
	public static function hasSameCategoryName($categoryName,$parentId,$categoryId =null){
		if(!$categoryName){
			throw new BusinessException('分类名称不能为空',Base::DATA_EMPTY_NAME);
		}
		
		$productCategoryFilter = new ProductCategoryFilter();
		if($categoryId){
			$productCategoryFilter->where(ProductCategoryFilter::CONDITION_FIELD_NAME_ID, Condition::CONDITION_NOTEQUAL, $categoryId);
		}
		$productCategoryFilter->where(ProductCategoryFilter::CONDITION_FIELD_NAME_PARENTID, Condition::CONDITION_EQUAL, $parentId);
		$productCategoryFilter->where(ProductCategoryFilter::CONDITION_FIELD_NAME_CATEGORY_NAME, Condition::CONDITION_EQUAL, $categoryName);
		$productCategoryFilter->where(ProductCategoryFilter::CONDITION_FIELD_NAME_STATUS, Condition::CONDITION_EQUAL, \haibao\cms\model\data\ProductCategoryDefine::STATUS_ENABLE);
		
		$data = new CategoryData();
		if($data->getAll($productCategoryFilter)){
			return false;
		}else{
			return true;
		}
	}
	/**
	 * 添加产品分类
	 * @param \haibao\cms\model\data\ProductCategoryDefine $productCategoryModel
	 */
	public static function addProductCategory($productCategoryModel){
		self::checkOperation(self::FUNC_CODE_PRODUCT_CATEGORY_ADD);
		
		if(!$productCategoryModel && !($productCategoryModel instanceof \haibao\cms\model\data\ProductCategoryDefine)){
			throw new BusinessException('产品分类不存在',Base::DATA_EMPTY);
		}
		if(!$productCategoryModel->CategoryName){
			throw new BusinessException('产品分类名称不能为空',Base::DATA_EMPTY_NAME);	
		}
		if(!self::hasSameCategoryName($productCategoryModel->CategoryName,$productCategoryModel->ParentId)){
			throw new BusinessException('产品分类名称已存在',Base::DATA_ERROR_NAME_EXIST);
		}
		$productCategoryModel->CreateBy = self::getCurrentUser()->Id;
		$productCategoryModel->CreateTime = new \DateTime();
		$productCategoryModel->Status = \haibao\cms\model\data\ProductCategoryDefine::STATUS_ENABLE;
		$productCategoryModel->OrderNo = self::getCurrentLevelOrder($productCategoryModel->ParentId);
		
		$data = new CategoryData();
		
		$id = $data->add($productCategoryModel);
		
		self::saveOperateHistory(self::FUNC_CODE_PAGE_CONTROL_CATEGORY_ADD, $productCategoryModel->toArray(true,true));
		return $id;
	}
	
	/**
	 * 获取当前层级排序
	 */
	public static function getCurrentLevelOrder($parentId){
		$categoryData = new CategoryData();
		$maxOrder = $categoryData->getMaxOrderByParentId($parentId);
		return $maxOrder ?$maxOrder + 1 : \haibao\cms\model\data\ProductCategoryDefine::DEFAULT_START_ORDERNO;
	}
	
	/**
	 * 保存产品分类
	 * @param \haibao\cms\model\data\ProductCategoryDefine $productCategoryModel
	 */
	public static function saveProductCategory($productCategoryModel){
		self::checkOperation(self::FUNC_CODE_PRODUCT_CATEGORY_SAVE);
		
		if(!$productCategoryModel && !($productCategoryModel instanceof \haibao\cms\model\data\ProductCategoryDefine)){
			throw new BusinessException('产品分类不存在',Base::DATA_EMPTY);
		}
		if(!$productCategoryModel->CategoryName){
			throw new BusinessException('产品分类名称不能为空',Base::DATA_EMPTY_NAME);
		}
		if(!self::hasSameCategoryName($productCategoryModel->CategoryName,$productCategoryModel->Id)){
			throw new BusinessException('产品分类名称已存在',Base::DATA_ERROR_NAME_EXIST);
		}
		if(!$productCategoryModel->OrderNo){
			throw  new BusinessException('分类排序不能为空',Base::DATA_EMPTY);
		}
		$data = new CategoryData();
		$data->save($productCategoryModel);
		
		self::saveOperateHistory(self::FUNC_CODE_PRODUCT_CATEGORY_SAVE, $productCategoryModel->toArray(true,true));
	}
	
	/**
	 * 删除分类
	 */
	public static function deleteCategory($categoryId){
		self::checkOperation(self::FUNC_CODE_PAGE_CONTROL_CATEGORY_DELETE);
		
		if(!$categoryId){
			throw new BusinessException('分类id不能为空',self::DATA_EMPTY_ID);
		}
		if(self::getAllChildrenCategory($categoryId)){
			throw new BusinessException('分类下有子分类不能删除');
		}
		$data = new CategoryData();
		$data->delete($categoryId);
		
		self::saveOperateHistory(self::FUNC_CODE_PRODUCT_CATEGORY_DELETE, array('Id'=>$categoryId));
	}
	/**
	 * 查找所有的孩子节点
	 */
	public static function getAllChildrenCategory($parentId){
		$data = new CategoryData();
		return $data->getAllChildrenCategory($parentId);
	}
	
	public static function getParentCategoryId($categoryId){
		$data = new CategoryData();
		return $data->getParentCategoryId($categoryId);
	}
	
	public static function getCategoryPorperty($categoryPropertyFilter){
		$data = new CategoryData();
		return $data->getAll($categoryPropertyFilter);
	}
	
	/**
	 * 查询下级分类
	 * @param int $parentId
	 */
	public static function getChildCategory($parentId){
		$data = new CategoryData();
		return $data->getChildCategory($parentId);
	}
	/**
	 * \haibao\cms\model\filter\ProductCategoryFilter $categoryFilter;
	 */
	public static function getCategoryByFilter($categoryFilter){
		$data = new CategoryData();
		return $data->getCategoryByFilter($categoryFilter);
	}
	
	
}